package cn.kgcnews.service.impl;

import java.util.List;

import cn.kgcnews.dao.CommentDao;
import cn.kgcnews.dao.impl.CommentDaoImpl;
import cn.kgcnews.pojo.NewsComment;
import cn.kgcnews.pojo.PageBean;
import cn.kgcnews.service.CommentService;

/**
 * 评论业务实现类
 * @author Administrator
 *
 */
public class CommentServiceImpl implements CommentService{

	@Override
	public boolean saveComment(NewsComment comment) {
	CommentDao cd =new CommentDaoImpl();
	boolean flag=false;//是否已经保存
	try {
		//判断添加的字是否有
		String a="共产党";
		String con=comment.getContext();
		
		if(con.contains(a)){
		    con=con.replaceAll("共产党", "***");
		    comment.setContext(con);
		    System.out.println("完成");
		}
		
		
		int result = cd.saveComment(comment);
		if(result>0){
			flag=true;
		}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		return flag;
	}

	@Override
	public List<NewsComment> findAllComment(int cnid,int currpage,int pagesize) {
		CommentDao cd = new CommentDaoImpl();
		PageBean<NewsComment> bean = new PageBean<NewsComment>();
		bean.setCurrPageNo(currpage);
		bean.setPageSize(pagesize);
		bean.setTotalCount(this.findcount(cnid));
		int seff=(currpage-1)*pagesize;
		try {
			return cd.findAllComment(cnid, seff, pagesize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int  findcount(int cnid) {
		CommentDao cd=new CommentDaoImpl();
		try {
			return  cd.findsize(cnid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

}
